//
//  LBFixedPicturesView.m
//  app
//
//  Created by 杨庆人 on 16/12/26.
//  Copyright © 2016年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBFixedPicturesView.h"
#import <Masonry/Masonry.h>
#import <LBCategory/LBCategory.h>

@interface LBFixedPicturesView (){
    
    CGSize _itemSize;
    CGFloat _left;
    CGFloat _right;
}

@property (nonatomic,strong)UILabel * bottomLabel;
@property (nonatomic,strong)LBFixedPhotoView * fixedPhotoView;
@property (nonatomic,assign)LBServiceOSSCode OSSCode;
@property (nonatomic,strong)NSArray * imageNameArray;
@property (nonatomic,copy)NSString * title;
@property (nonatomic,copy)NSString * bottomText;
@property (nonatomic,copy)NSString * chatStr;

@end
@implementation LBFixedPicturesView


- (instancetype)initWithTitle:(NSString *)title bottomText:(NSString *)bottomText charStr:(NSString *)charStr ItemSize:(CGSize)itemSize left:(CGFloat)left right:(CGFloat)right imageNameArray:(NSArray *)imageNameArray OSSCode:(LBServiceOSSCode)OSSCode {
    if (self = [super init]) {
        _title = title;
        _bottomText = bottomText;
        _itemSize = itemSize;
        _left = left;
        _right = right;
        _OSSCode = OSSCode;
        _imageNameArray = imageNameArray;
        _chatStr = charStr;
        [self setFixedPicturesView];
    }
    return self;
}


- (instancetype)initWithTitle:(NSString *)title bottomText:(NSString *)bottomText charStr:(NSString *)charStr  imageNameArray:(NSArray *)imageNameArray OSSCode:(LBServiceOSSCode)OSSCode {

    return [self initWithTitle:title bottomText:bottomText charStr:charStr ItemSize:CGSizeMake(LBPicturesMargin, LBPicturesMargin) left:15 right:15 imageNameArray:imageNameArray OSSCode:OSSCode];
}

- (NSMutableArray <LBUploadPhotoModel *>*)dataArray {
    
    NSMutableArray <LBUploadPhotoModel *> * array = [[NSMutableArray <LBUploadPhotoModel *> alloc] init];
    for (LBUploadPhotoModel * model in self.fixedPhotoView.dataArray) {
        if (model.urlString.length || model.objectKey.length || model.image) {
            [array addObject:model];
        }
    }
    return array;
}

- (LBFixedPhotoView *)fixedPhotoView {
    if (!_fixedPhotoView) {
        _fixedPhotoView = [[LBFixedPhotoView alloc] initWithItemSize:_itemSize left:_left right:_right imageNameArray:_imageNameArray OSSCode:_OSSCode];
    }
    return _fixedPhotoView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:12];
        _titleLabel.textColor = [UIColor colorWithR:134 G:134 B:134 A:1];
    }
    return _titleLabel;
}
- (UILabel *)bottomLabel {
    if (!_bottomLabel) {
        _bottomLabel = [[UILabel alloc] init];
        _bottomLabel.font = [UIFont systemFontOfSize:12];
        _bottomLabel.textColor = [UIColor colorWithRed:74/255.0 green:144/255.0 blue:226/255.0 alpha:1];
        _bottomLabel.text = _bottomText;
    }
    return _bottomLabel;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    

}
- (void)setFixedPicturesView {
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.fixedPhotoView];
    [self addSubview:self.bottomLabel];
    
    if (_chatStr.length) {
        NSString * string = [NSString stringWithFormat:@"%@%@",_title,_chatStr];
        NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:string];
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:74/255.0 green:144/255.0 blue:226/255.0 alpha:1] range:[string rangeOfString:_chatStr]];
        self.titleLabel.attributedText = attrStr;
    }
    else {
        self.titleLabel.text = _title;
    }

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.height.mas_equalTo(17);
        make.left.mas_equalTo(self).offset(_left);
        make.right.mas_equalTo(self).offset(-_right);
    }];
    [self.fixedPhotoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(5);
    }];
    [self.bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.fixedPhotoView.mas_bottom).offset(5);
        make.left.mas_equalTo(self).offset(_left);
        make.right.mas_equalTo(self).offset(-_right);
        make.height.mas_equalTo(17);
        make.bottom.mas_equalTo(self.mas_bottom);
    }];
}

- (void)setFixedPicturesViewWithImageUrlArray:(NSArray *)imageUrlArray {
    NSMutableArray * array;
    if (!imageUrlArray) {
        array = [NSMutableArray arrayWithArray:@[]];
    }
    else {
        array = [NSMutableArray arrayWithArray:imageUrlArray];
    }
    [self.fixedPhotoView setFixedPhotoViewWithImageUrlArray:array];
}

@end
